<?php
/**
 * 所有文章管理
 * User: Backli
 * Date: 12-12-4
 * Time: 下午4:00
 */
class ArticleAction extends CommonAction{
    /*
     * 文章列表
     */
    function index(){
        $art = D("Articles");
        $data = $art->select();
        $this->arts = $data;
        $this->display();
    }
    /*
     * 添加文章
     */
    function add(){
        if($_POST){
            $art = D("Articles"); //使用视图模型
            if($art->create()){
//                dump($art->create());
                $result = $art->add();
                if($result){
                    $this->success("添加文章成功!",__URL__."/");
                }else{
                    $this->error("操作失败！".$art->getError(),null,30);
                }
            }else{
                $this->error("操作失败！".$art->getError());
            }
        }else{
            $cate = D("Category");
            $cdata = $cate->select();
            $this->cates = $cdata;
            $this->pubdate = $this->getTime(); //getTime()存在于CommonAction中
            $this->display();
        }
    }
    /*
     * 删除文章
     */
    function delete(){
        $id = trim($_GET['id']);
        if($id){
            $art = D("Articles");
            $result = $art->delete($id);
            if($result>0){
                $this->success("删除成功，id编号为：".$id." ; 共删除".$result."条记录");
            }else{
                $this->error("操作失败，".$art->getError());
            }
        }else{
            $this->error("无参数操作");
        }
    }
    /*
     * 编辑文章
     */
    function edit(){
        $arts = D("Articles");
        if($_POST['title']){
            $arts->create();
            if($arts->save()){
                $this->success("修改成功!",__URL__."/");
            }else{
                dump($arts->create());
                $this->success("修改失败，错误：".$arts->getError(),null,20);
            }
        }else{
            $id = trim($_GET['id']);
            if(!$id){
                $this->error("非法操作");
            }
            $category = D("Category");
            $this->cates = $category->select();
            $this->art = $arts->find($id);
            $this->display();
        }
    }
}
